package com.kkb.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.vo.TeamConditionParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeamService {

    @Resource
    private TeamMapper teamMapper;

    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Team> findByPage(Integer pageNum, Integer pageSize,TeamConditionParam param){

        //多条件
        TeamExample example = new TeamExample();

        //创建条件的容器
        TeamExample.Criteria criteria = example.createCriteria();

        //只查询条件isDel为0的数据
        criteria.andIsDelEqualTo(0);

        //模糊查询条件
        if (param !=null ){
            //模糊查询条件队名
            //param.getTeamName().trim()去除字符串头尾空格
            if (param.getTeamName() !=null && !"".equals(param.getTeamName().trim())){
                criteria.andTeamNameLike("%"+param.getTeamName().trim()+"%");
            }

            //模糊查询条件中文队名
            if (param.getChineseName() !=null && !"".equals(param.getChineseName().trim())){
                criteria.andChineseNameLike("%"+param.getChineseName().trim()+"%");
            }

            //模糊查询条件教练
            if (param.getCoach() !=null && !"".equals(param.getCoach().trim())){
                criteria.andCoachLike("%"+param.getCoach().trim()+"%");
            }

            //模糊查询条件起始日期
            if (param.getBeginDate() !=null ){
                criteria.andCreateTimeGreaterThanOrEqualTo(param.getBeginDate());
            }

            //模糊查询条件截止日期
            if (param.getEndDate() !=null ){
                criteria.andCreateTimeLessThanOrEqualTo(param.getEndDate());
            }

            //模糊查询条件地区
            if (param.getArea() != null && param.getArea()!=-1){
                criteria.andAreaEqualTo(param.getArea());
            }



        }

        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Team> list = teamMapper.selectByExample(example);

        return new PageInfo<>(list);

    }

    //添加球队
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addTeam(Team team){

        return teamMapper.insertSelective(team);
    }

    //根据主键查询球队信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Team findById(int teamId){
        return teamMapper.selectByPrimaryKey(teamId);
    }

    //根据主键更新球队信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateById(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    //根据主键删除球队信息---此处为逻辑删除
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteById(int teamId){

        Team team = teamMapper.selectByPrimaryKey(teamId);
        team.setIsDel(1); //将isDel设为1即作为删除标识

        return teamMapper.updateByPrimaryKeySelective(team);

    }



}
